package com.skyworth.skymanualview;
import java.util.ArrayList;
import com.skyworth.skymanualActivity.GuideListActivity;
import android.content.Context;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.FrameLayout;
import android.widget.Scroller;
public class MetroLayout extends FrameLayout implements OnGestureListener {
	private ArrayList<ArrayList<Integer>> container;
	private ArrayList<Integer> ids, pre_ids;
	private int focusId = 0;
	private Scroller mScroller;
	public static int APP = 1, CLOUD = 1, HOME = 2;
	public int Margin_Num = (int) (40*GuideListActivity.DIV_H);
	private int id, size_r = 0;
	private FrameLayout.LayoutParams lp;
	public int first_x = 0, first_y = 0;
	private int x = 0, y = 0;
	private GestureDetector mGestureDetector;
	private Context mContext;
	public MetroLayout(Context context) {
		super(context);
		mContext = context;
		mScroller = new Scroller(context,new LinearInterpolator(){});
		container = new ArrayList<ArrayList<Integer>>();
		mGestureDetector = new GestureDetector(this);
		this.setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				return mGestureDetector.onTouchEvent(event);
			}
		});
		this.setLongClickable(true);
	}

	public void cleanContainer() {
		if (container != null) {
			container.clear();
		}
		if (ids != null) {
			ids.clear();
		}
		if (pre_ids != null) {
			pre_ids.clear();
		}
	}

	public void setFocusItem(int id) {
		for (int i = 0; i < this.getChildCount(); i++) {
			if (this.getChildAt(i).getId() == id) {
				this.getChildAt(i).requestFocus();		
				Log.i("lhy","****this.getChildAt("+i+").requestFocus()");
			}
		}
	}

	public void setFocus(String val) {

	}
	/**
	 * 
	 */
	public void setSize(int r) {
		size_r = r;
		first_x = (int) (70*GuideListActivity.DIV_W/GuideListActivity.density);
		first_y = (int) (70*GuideListActivity.DIV_H/GuideListActivity.density);
		for (int i = 0; i < size_r; i++) {
			container.add(new ArrayList<Integer>());
		}		
	}

	public boolean addSubView(MetroView m_v, int row, int cols) {
		//Log.v("lhy", m_v.toString());

		id = row * 10 + cols;

		if (row < 0 || row >= size_r || cols < 0) {
			return false;
		}

		int size = this.getChildCount();
		for (int i = 0; i < size; i++) {
			if (this.getChildAt(i).getId() == id) {
				return false;
			}
		}
		ids = container.get(row);
		if (cols > ids.size()) {
			return false;
		}

		ids.add(cols, id);

		if (cols == 0) {
			x = first_x;
		} else {
			MetroView preView = (MetroView) this
					.findViewById(ids.get(cols - 1));
			x = (int) (preView.getXPostion() + preView.getWidthSize()
					+ Margin_Num - (2 * 15 ));
		}

		if (row == 0) {
			y = first_y;
		} else {
			pre_ids = container.get(row - 1);
			int pre_x = 0;
			for (int i = 0; i < pre_ids.size(); i++) {
				MetroView view = (MetroView) this.findViewById(pre_ids.get(i));
				pre_x = view.getXPostion() + view.getWidthSize();
				if (x < pre_x) {
					y = (int) (view.getYPostion() + view.getHeightSize()
							+ Margin_Num - (2 * 15));
					break;
				}
			}
		}
		m_v.setId(id);

		lp = new FrameLayout.LayoutParams(m_v.getWidthSize(),
				m_v.getHeightSize());
		lp.gravity = Gravity.TOP | Gravity.LEFT;
		lp.setMargins(x, y, 0, 0);
		m_v.setXPosition(x);
		m_v.setYPosition(y);
		this.addView(m_v, lp);
		return true;
	}



	// change view
	public boolean ReplaceView(MetroView newView, int row, int cols) {
		MetroView preView = (MetroView) this.findViewById(container.get(row)
				.get(cols));
		this.removeView(preView);
		container.get(row).remove(cols);
		addSubView(newView, row, cols);
		return true;
	}

	// delete view
	public boolean DeleteView(int row, int cols) {
		Log.v("zhoufeng'an", "DeleteView");
		MetroView[] d_view = new MetroView[container.get(row).size() - cols];
		MetroView[] a_view = new MetroView[container.get(row).size() - cols];
		Log.e("MetroView[]", "a_view.length====: " + a_view.length);
		if (cols < container.get(row).size()) {
			for (int i = 0; i < (container.get(row).size() - cols); i++) {
				d_view[i] = (MetroView) this.findViewById(container.get(row)
						.get(cols + i));
				a_view[i] = (MetroView) this.findViewById(container.get(row)
						.get(cols + i));
				this.removeView(d_view[i]);
			}
			for (int j = 0; j < a_view.length - 1; j++) {
				this.addSubView(a_view[j + 1], row, cols + j);
			}
		}
		return true;
	}

	// query view
	public MetroView QueryView(int row, int cols) {
		int view_ID = 0;
		view_ID = row * 10 + cols;
		MetroView queryView = (MetroView) this.findViewById(view_ID);
		return queryView;
	}

	public void setMetroViewEnabled(boolean hasNet) {
		MetroView v = null;
		for (int i = 0; i < this.getChildCount(); i++) {
			v = QueryView(i);
			if (v != null) {
				if (hasNet == false) {
					v.setEnabled(false);
				} else {
					v.setEnabled(true);
				}

			}
		}
	}

	// query view
	public MetroView QueryView(int index) {
		if (index < 0) {
			return null;
		}
		int r = container.size();
		int range = 0;
		for (int i = 0; i < r; i++) {
			range = container.get(i).size();

			if (index < range) {
				try {
					return (MetroView) this.findViewById(container.get(i).get(
							index));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			index -= container.get(i).size();
		}
		return null;
	}

	public void themeChanged() {

	}

	public void setFocusId(int i) {
		focusId = i;
	}

	public int getFocusId() {
		return focusId;
	}

	@Override
	public boolean onDown(MotionEvent e) {
		return true;
	}

	@Override
	public void onShowPress(MotionEvent e) {
	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		return false;
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		return false;
	}

	@Override
	public void onLongPress(MotionEvent e) {
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {

		return true;
	}

	@Override
	public void computeScroll() {
		if (mScroller.computeScrollOffset()) {
			scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
			postInvalidate();
		}
		super.computeScroll();
	}

	private void smoothScrollBy(int dx, int dy) {
		mScroller.startScroll(mScroller.getFinalX(), mScroller.getFinalY(), dx, dy, 400);
		invalidate();
	}

	public void startUsingVelocity(int initialVelocity) {
		smoothScrollBy(initialVelocity, 0);
	}
    private void moveFocusLength(int verX,String direction){
    	if("R".equals(direction)){
    		smoothScrollBy(verX,0);
    	}else{
    		smoothScrollBy(-verX,0);
    	}
    }
	
	int countM = 0;
	public void startUsingVelocity(String direction) {

	}
	
	public boolean isLastView(View view){
		int viewId = view.getId();
		if(String.valueOf(viewId).startsWith("3")){
			int temp  = viewId-3000;
			if(temp<10){
				if(findViewById(viewId+1)==null&&findViewById(viewId+1*10+1)==null){
					View nextView = findViewById(viewId+10);
				   if(nextView.getRight()>view.getRight()){
					   return false;
				   }else{
					   return true;	 
				   }
				}
			}else{
				if(findViewById(viewId+1)==null&&findViewById(viewId-1*10+1)==null){	
				  View upView = findViewById(viewId-10);
					if(upView.getRight()>view.getRight()){
						   return false;
					  }else{
						   return true;	 
					 }
				}
			}
		}
		return false;
	}
}
